package net.emsquared.gamedev;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.utils.Logger;
import net.emsquared.gamedev.screens.IScreen;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

/**
 * Code is not copyrighted...I don't think.
 *
 * @author Matt Meza (mmeza84@gmail.com)
 */

public class TestGame extends InputAdapter implements ApplicationListener
{
   private final Logger logger = new Logger("TestGame");

   public static final float GAME_WIDTH = 800;
   public static final float GAME_HEIGHT = 480;

   private IScreen activeScreen;

   private Map<String, IScreen> screenMap;

   public void create()
   {
      Gdx.input.setInputProcessor(this);
      screenMap = new HashMap(3);

      startScreen("SplashScreen");
   }

   public void startScreen(String aScreenName)
   {
      if(activeScreen != null)
      {
         activeScreen.pause();
      }

      IScreen screen = getScreenByName(aScreenName);
      screenMap.put(aScreenName, screen);

      activeScreen = screen;
      activeScreen.initialize(this);
      activeScreen.start();
   }

   public IScreen getScreenByName(String aScreenName)
   {
      try
      {
         String screenPrefix = "net.emsquared.gamedev.screens.";
         Class screenClass = Class.forName(screenPrefix + aScreenName);
         Constructor constructor = screenClass.getConstructor();

         Object screenObject = constructor.newInstance();
         return (IScreen)screenObject;
      }
      catch (Exception e)
      {
         logger.log("Exception instantiating class named " + aScreenName);
      }

      return null;
   }

   public void resize(int aWidth, int aHeight)
   {
      /**
       * Handle resizing of the world on screen rotate...not sure what that entails.
       * We could probably not let the rotate the screen, otherwise I think there's some calculations involved.
       **/
   }

   public void render()
   {
      activeScreen.update();

      activeScreen.draw();
   }

   private void checkCollisions()
   {
      //        TODO find a way to get the region name from this...
   }

   public void pause()
   { /** Save game state to restore on resume **/}

   public void resume()
   { /** Restore saved state from a Pause event **/}

   public void dispose()
   { /** Dispose of things that need manual disposal...find out what those are. **/}
}
